<?php

namespace app\admin\controller;

use think\Db;

class Live extends Common
{
    public function lists()
    {
        $where = ['t1.status' => 1];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where['t2.' . $search_type] = $search_name;
        }

        $data = Db::name('live t1')
            ->field('t1.*, t2.name, t2.phone')
            ->join('user t2', 't2.id = t1.user_id')
            ->where($where)->order('t1.sort desc, t1.tuijian desc, t1.id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addLive()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (isset($post['tuijian_expire']) && $post['tuijian_expire']) {
                $post['tuijian_expire'] = strtotime($post['tuijian_expire']);
            }

            if (!$id) {
                $db = Db::name('live')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('live')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('live')->find($id);
                if ($name) {
                    if ($name['tuijian_expire']) {
                        $name['tuijian_expire'] = date('Y-m-d H:i:s', $name['tuijian_expire']);
                    }

                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }

    public function medal()
    {
        $data = Db::name('live_medal')->order('id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addMedal()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('live_medal')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('live_medal')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('live_medal')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }

    public function config()
    {
        $data = Db::name('live_star_config')->order('id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addConfig()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('live_star_config')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('live_star_config')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('live_star_config')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }

    public function userStar()
    {
        $where = ['t1.id' => ['gt', 0]];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where['t2.' . $search_type] = $search_name;
        }

        $data = Db::name('live_user_star t1')
            ->field('t1.*, t2.name, t2.phone, t3.preview')
            ->join('user t2', 't2.id = t1.user_id')
            ->join('gift t3', 't3.id = t1.gift_id', 'LEFT')
            ->where($where)
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }
}
